package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.validator.ValidatorAdd;
import com.heima.model.common.validator.ValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 1、接收前端请求
 * 2、调用Service（业务逻辑）
 * 3、返回结果
 * @Description:
 * @Version: V1.0
 *
 * code:
 * 400： 前端传递参数有问题
 * 404： 前端url路径有问题
 * 405： 请求方式不匹配
 *
 */
@Api(value = "频道管理", tags = "channel", description = "频道管理API")
@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController {


    @Autowired
    AdChannelService channelService;


    @ApiOperation("频道分页查询")
    @PostMapping("list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto dto ) {
        return channelService.findByNameAndPage(dto);
    }


    @ApiOperation("频道查询所有")
    @GetMapping("channels")
    public ResponseResult channels() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }


    @ApiOperation("频道查询所有")
    @GetMapping("findAllChannels")
    public List<AdChannel> findAllChannels() {
        List<AdChannel> list = channelService.list();
        return list;
    }



    @ApiOperation("频道新增")
    @PostMapping("save")
    public ResponseResult insert(@RequestBody @Validated(ValidatorAdd.class) AdChannel adChannel) {
        return channelService.insert(adChannel);
    }

    @ApiOperation("频道修改[修改频道状态]")
    @PostMapping("update")
    public ResponseResult update(@RequestBody @Validated(ValidatorUpdate.class) AdChannel adChannel) {
        return channelService.update(adChannel);
    }


    @ApiOperation("根据ID删除频道")
    @GetMapping("del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        return channelService.delete(id);
    }


}
